package com.c.util;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlRunner;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

public class HutoolTest {

	public static DataSource ds = new SimpleDataSource("jdbc:mysql://10.10.3.240:3306/swork?characterEncoding=utf-8",
			"root", "root123");

	public static void main(String[] args) {
		// 1.读取文件
		ExcelReader reader;
		reader = ExcelUtil.getReader(FileUtil.file("C:\\Users\\吴晨炀\\Desktop\\app.xls"), "协议说明");
		List<Map<String, Object>> content = reader.readAll();
		System.out.println("读取了" + content.size() + "条 数据...");
		// 2.遍历 “行”
		try {
			for (int i = 0; i < content.size(); i++) {
				Thread.sleep(100);
				System.out.println("第" + i + "次循环");
				checkAppName(ds, String.valueOf(content.get(0).get("应用")), String.valueOf(content.get(0).get("应用说明")));
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static void checkAppName(DataSource ds, String appName, String detail) {
		SqlRunner runner = SqlRunner.create(ds);
		try {
			List<Entity> res = runner.findBy("app", "app", appName);
			int size = res.size();
			if (size == 0) {
				// 表中不存在
				Long id = runner.insertForGeneratedKey(Entity.create("app").set("app", appName).set("detail", detail));
				System.out.println("插入新数据id:" + id + "  应用名称:" + appName + " 应用说明：" + detail);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
